#include "JProject.h"
#include <QFileInfo>
#include <QDir>

JProject::JProject()
	:m_bModified(false),
	m_Name("Untitled"),
	m_Path("")
{
	m_pItemModel = new QStandardItemModel(this);
	m_pProjectItem = m_pItemModel->invisibleRootItem();

	QStandardItem* item = new QStandardItem(m_Name);
	item->setEditable(false);
	item->setAccessibleDescription("Project");
	item->setData(m_Path);
	m_pProjectItem->appendRow(item);
	m_pProjectItem = item;
}

JProject::~JProject()
{
}

JProject& JProject::Instance()
{
	static JProject m_Project;
	return m_Project;
}

QString JProject::Name()
{
	//QString name = strippedName(m_FilePath);
	//name.chop(4); 
	return m_Name;
}

QString JProject::FileName()
{
	return QFileInfo(m_Path).fileName();
}

QString JProject::FilePath()
{
	return QFileInfo(m_Path).filePath();
}

bool JProject::Load( const QDomElement& element )
{
	return loadFromXML(element);
}

void JProject::New( const QString& name, const QString& filePath )
{
	m_Name = name;
	m_Path = filePath;
	QDir::setCurrent(m_Path);

	// clear current model
	m_pItemModel->clear();
	// create the top level item with project name
	QStandardItem* item = new QStandardItem(name);
	item->setEditable(false);
	item->setAccessibleDescription("Project");
	item->setData(filePath);
	m_pProjectItem->appendRow(item);
	m_pProjectItem = item;
}

QDomElement JProject::Save(QDomDocument& document)
{
	return saveToXML(document);
}

void JProject::Add3DModel(const QString& filePath)
{
	QIcon icon;
	icon.addFile(QString::fromUtf8(":/JProject/Resources/Objects.png"));
	QStandardItem* item = new QStandardItem(icon, QFileInfo(filePath).fileName());
	item->setEditable(false);
	item->setAccessibleDescription("STL");
	item->setData(filePath);
	// get standard item to do the append
	m_pProjectItem->appendRow(item);
}

void JProject::AddKinematics(const QString& filePath)
{
	QIcon icon;
	icon.addFile(QString::fromUtf8(":/JProject/Resources/jts.png"));
	QStandardItem* item = new QStandardItem(icon, QFileInfo(filePath).fileName());
	item->setEditable(false);
	item->setAccessibleDescription("JTS");
	item->setData(filePath);
	// get JTS item to do the append
	QStandardItem* item2 = m_pItemModel->itemFromIndex(m_Index);
	item2->appendRow(item);
}

QDomElement JProject::saveToXML( QDomDocument& document )
{
	m_RootItem = document.createElement("Project");
	m_RootItem.setAttribute("Name", m_Name);
	m_RootItem.setAttribute("Path",m_Path);
	document.appendChild(m_RootItem);

	if (!m_3DModelList.isEmpty())
	{
		QDomElement element = document.createElement("ModelList");
		element.setAttribute("ListSize",m_3DModelList.size());
		for (int i = 0; i < m_3DModelList.size(); ++i)
		{
			element.setAttribute(QString("Model%0").arg(i),m_3DModelList[i]);
		}
		m_RootItem.appendChild(element);
	}

	return m_RootItem;
}

bool JProject::loadFromXML( const QDomElement& element )
{
	// clear current model
	m_pItemModel->clear();

	QDomNode n = element.firstChild();
	while( !n.isNull() )
	{
		QDomElement e = n.toElement();
		if( !e.isNull() )
		{
			if( e.tagName() == "Project" )
			{
				if (e.hasAttribute("Name"))
				{
					m_Name = e.attribute("Name");
					// create the top level item with project name
					QStandardItem* item = new QStandardItem(m_Name);
					m_pProjectItem->appendRow(item);
					m_pProjectItem = item;
				}
				if (e.hasAttribute("Path"))
				{
					m_Path = e.attribute("Path");
					// set current directory to m_Path
					QDir::setCurrent(m_Path);
				}
				if (e.hasChildNodes())
				{
					QDomNode n2 = e.firstChild();
					while(!n2.isNull())
					{
						QDomElement e2 = n2.toElement();
						if (!e2.isNull())
						{
							if (e2.tagName() == "ModelList")
							{
								// warning: assuming all the attributes exists
								QString tempS = e2.attribute("ListSize");
								int size = tempS.toInt();
								m_3DModelList.clear();
								for (int i = 0; i < size; ++i)
								{
									// call the load function for models maybe send a signal with the string to widget
									// emit something
									// append them to the m_3DModelList
									m_3DModelList.append(e.attribute(QString("Model%0").arg(i)));
								}
							}
						}
						n2 = n2.nextSibling();
					}
				}
				// more...
				// create the std model item.
			}
			// other tags...
		}

		n = n.nextSibling();
	}
	return true;
}

void JProject::updateIndex( QModelIndex index )
{
	m_Index = index;
}

QModelIndex JProject::selectionIndex()
{
	return m_Index;
}

QStandardItemModel* JProject::itemModel()
{
	return m_pItemModel;
}

QStandardItem* JProject::itemFromIndex( QModelIndex index )
{
	return m_pItemModel->itemFromIndex(index);
}